package com.hospes.rmes.managers;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.util.Log;
import com.hospes.rmes.models.News;
import com.hospes.rmes.utils.Utils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Andrew Khloponin
 * Date: 28.09.11
 * Time: 21:46
 */
public class NewsManager {
	private static NewsManager self;
	private static final String LOG_TAG = "NewsManager";

	public static String tableName;
	public static final String colNid = "nid";
	public static final String colDate = "date";
	public static final String colTitle = "title";
	public static final String colFiles = "files";
	public static final String colText = "text";
	public static final String colTeaser = "teaser";

	public NewsManager() {
		self = this;
	}

	public static NewsManager getInstance() {
		if (null == self)
			new NewsManager();
		return self;
	}

	public static void destroyInstance() {
		self = null;
		Log.e(LOG_TAG, "NewsManager.destroyInstance()");
	}

	public int saveNewsToDataBase(SQLiteDatabase db, News item) {
		ContentValues values = new ContentValues();
		values.put(colDate, Utils.FromDateToString(item.date));
		values.put(colTitle, item.title);
		values.put(colFiles, item.files);
		values.put(colText, item.text);
		values.put(colTeaser, item.teaser);

		if (item.nid == -1) {
			item.nid = (int) db.insert(tableName, null, values);
		} else {
			db.update(tableName, values, "_id=" + item.nid, null);
		}
		return item.nid;
	}

	public ArrayList<News> getAllNewsFromDataBase(SQLiteDatabase db) {
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		qb.setTables(tableName);
		Cursor cursor = qb.query(db, null, null, null, null, null, null);
		return fillObjects(cursor);
	}

	private ArrayList<News> fillObjects(Cursor cursor) {
		ArrayList<News> elements = new ArrayList<News>();
		if (cursor.moveToFirst()) {
			int colIndexId = cursor.getColumnIndex("_id");
			int colIndexDate = cursor.getColumnIndex(colDate);
			int colIndexTitle = cursor.getColumnIndex(colTitle);
			int colIndexFiles = cursor.getColumnIndex(colFiles);
			int colIndexText = cursor.getColumnIndex(colText);
			int colIndexTeaser = cursor.getColumnIndex(colTeaser);
			do {
				try {
					News element = new News();
					element.nid = cursor.getInt(colIndexId);
					element.date = Utils.FromStringToDate(cursor.getString(colIndexDate));
					element.title = cursor.getString(colIndexTitle);
					element.files = cursor.getString(colIndexFiles);
					element.text = cursor.getString(colIndexText);
					element.teaser = cursor.getString(colIndexTeaser);
					elements.add(element);
				} catch (Exception e) {
				}
			} while (cursor.moveToNext());
		}
		cursor.close();
		return elements;
	}

	public void saveAll(SQLiteDatabase db, ArrayList<News> items) {
		for (News item : items) {
			saveNewsToDataBase(db,item);
		}
	}

	public ArrayList<News> getNewsListFromJSON(JSONArray array) {
		ArrayList<News> result;
		try {
			result = new ArrayList<News>();
			for (int i = 0; i < array.length(); i++) {
				JSONObject node = array.getJSONObject(i);
				JSONObject line = node.getJSONObject("node");
				News item = new News();
				item.date = Utils.FromStringToDate(line.getString(colDate));
				item.title = line.getString(colTitle);
				if (line.getString(colFiles).contains(",")) {
					item.files = line.getString(colFiles).substring(0, line.getString(colFiles).indexOf(","));
				} else {
					item.files = line.getString(colFiles);
				}
				item.text = line.getString(colText);
				item.teaser = line.getString(colTeaser);
				result.add(item);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = null;
		}
		return result;
	}
}
